package com.lottery.ruok.lottrey.dialog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.ChooseDateActivity;
import com.lottery.ruok.lottrey.adapter.FiltrateAdapter;
import com.lottery.ruok.lottrey.eventbusObject.FilterGetLogs;
import com.lottery.ruok.lottrey.fragment.BaseFragment;
import com.lottery.ruok.lottrey.retrofitModule.BaseModel;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetCashTypeRequest;
import com.lottery.ruok.lottrey.retrofitModule.request.GetRechargeTypeRequest;
import com.lottery.ruok.lottrey.retrofitModule.request.GetWithdrawalsTypeRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetCashTypeResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import ui.NoScrollGridView;

public class FiltrateDialog extends BaseFragment {
    @BindView(R.id.time1)
    TextView time1;
    @BindView(R.id.time2)
    TextView time2;
    @BindView(R.id.grid_view)
    NoScrollGridView gridView;
    @BindView(R.id.confirm)
    TextView confirm;
    private int type;
    private ArrayList<GetCashTypeResponse> getCashTypeResponses;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.filtrate_dialog_layout, null);

        ButterKnife.bind(this, v);
        getList();

        return v;
    }

    private void getList() {
        String startTime = getArguments().getString("startTime");
        String endTime = getArguments().getString("endTime");
        type = getArguments().getInt("type");
        time1.setText(startTime);
        time2.setText(endTime);
        Observable<BaseModel<ArrayList<GetCashTypeResponse>>> result;
        final boolean chooseType;
        if(type == 1){
            result = RetrofitBase.getRetrofit().create(GetCashTypeRequest.class).getResult();
            chooseType = true;
        } else if(type == 2){
            result = RetrofitBase.getRetrofit().create(GetRechargeTypeRequest.class).getResult("recharge", UserInfo.getToken());
            chooseType = false;
        } else{
            result = RetrofitBase.getRetrofit().create(GetWithdrawalsTypeRequest.class).getResult("withdraw", UserInfo.getToken());
            chooseType = false;
        }

        result.compose(RxHelper.<ArrayList<GetCashTypeResponse>>handleResult())
                .subscribe(new RxSubscribe<ArrayList<GetCashTypeResponse>>(getActivity()) {
            @Override
            protected void _onNext(ArrayList<GetCashTypeResponse> getCashTypeResponse) {
                getCashTypeResponses = getCashTypeResponse;

                if(getArguments().getString("types") != null) {
                    String[] types = getArguments().getString("types").split(",");
                    for(String d : types){
                        for(GetCashTypeResponse t : getCashTypeResponses){
                            if(d.equals(t.getId())){
                                t.setChoose(true);
                                break;
                            }
                        }
                    }
                }

                if(getActivity() != null){
                    gridView.setAdapter(new FiltrateAdapter(getCashTypeResponses, getActivity(), chooseType));
                }
            }

            @Override
            protected boolean showDialog() {
                return false;
            }
        });
    }

    @OnClick({R.id.close, R.id.confirm, R.id.time1, R.id.time2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close:
                getFragmentManager().popBackStack();
                break;
            case R.id.confirm:
                FilterGetLogs filterGetLogs = new FilterGetLogs();
                StringBuilder types = null;
                if(getCashTypeResponses != null) {
                    for (GetCashTypeResponse getCashTypeResponse : getCashTypeResponses) {
                        if (getCashTypeResponse.isChoose()) {
                            if (types == null) {
                                types = new StringBuilder(getCashTypeResponse.getId());
                            } else {
                                types.append(",").append(getCashTypeResponse.getId());
                            }
                        }
                    }
                }
                if(types != null) {
                    filterGetLogs.setTypes(types.toString());
                }
                filterGetLogs.setStart_time(time1.getText().toString().equals("") ? null : time1.getText().toString());
                filterGetLogs.setEnd_time(time2.getText().toString().equals("") ? null : time2.getText().toString());
                EventBus.getDefault().post(filterGetLogs);
                getFragmentManager().popBackStack();
                break;
            case R.id.time1:
                Intent intent = new Intent(getActivity(), ChooseDateActivity.class);
                if(type==3){
                    if(!time2.getText().toString().equals("")){
                        intent.putExtra("endTime", time2.getText().toString() + "-01 00:00:00");
                    }
                    if(!time1.getText().toString().equals("")){
                        intent.putExtra("time", time1.getText().toString() + "-01 00:00:00");
                    }
                }else{
                    if(!time2.getText().toString().equals("")){
                        intent.putExtra("endTime", time2.getText().toString() + " 00:00:00");
                    }
                    if(!time1.getText().toString().equals("")){
                        intent.putExtra("time", time1.getText().toString() + " 00:00:00");
                    }
                }

                intent.putExtra("type",type);
                startActivityForResult(intent, 101);
                getActivity().overridePendingTransition(R.anim.anim_enter_from_bottom, R.anim.activity_stay);
                break;
            case R.id.time2:
                Intent intent1 = new Intent(getActivity(), ChooseDateActivity.class);
                if(type==3){
                    if(!time1.getText().toString().equals("")){
                        intent1.putExtra("startTime", time1.getText().toString() + "-01 00:00:00");
                    }
                    if(!time2.getText().toString().equals("")){
                        intent1.putExtra("time", time2.getText().toString() + "-01 00:00:00");
                    }
                }else{
                    if(!time1.getText().toString().equals("")){
                        intent1.putExtra("startTime", time1.getText().toString() + " 00:00:00");
                    }
                    if(!time2.getText().toString().equals("")){
                        intent1.putExtra("time", time2.getText().toString() + " 00:00:00");
                    }
                }

                intent1.putExtra("type",type);
                startActivityForResult(intent1, 102);
                getActivity().overridePendingTransition(R.anim.anim_enter_from_bottom, R.anim.activity_stay);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
            if(type==3){
                time1.setText(data.getStringExtra("date"));
                time2.setText(data.getStringExtra("date"));
            }else{
                time1.setText(data.getStringExtra("date").split(" ")[0]);
            }


        } else if(requestCode == 102 && resultCode == Activity.RESULT_OK){
            if(type==3){
                time1.setText(data.getStringExtra("date"));
                time2.setText(data.getStringExtra("date"));
            }else{
                time2.setText(data.getStringExtra("date").split(" ")[0]);
            }

        }
    }
}